<template>
  <div class="app-container">
    <el-form
      ref="form"
      class="form"
      :rules="rules"
      :model="form"
      label-width="80px"
    >
      <el-form-item label="客户名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入商品名称"></el-input>
      </el-form-item>

      <el-form-item label="客户电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入商品价格"></el-input>
      </el-form-item>

      <el-form-item label="客户区域" prop="city">
        <el-cascader
          style="width: 100%"
          size="large"
          :options="options"
          v-model="selectedOptions"
          @change="handleChange"
        >
        </el-cascader>
      </el-form-item>

      <el-form-item label="详细地址" prop="address">
        <el-input
          type="textarea"
          v-model="form.address"
          placeholder="请输入详细地址"
        ></el-input>
      </el-form-item>

      <el-form-item label="客户备注">
        <el-input
          v-model="form.remark"
          :maxlength="4"
          placeholder="请输入客户备注"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{
          form.id ? "保存修改" : "立即创建"
        }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addOrEditCustomer, getCustomerDetail } from "@/api/customer/index";
import { regionData } from "element-china-area-data";

export default {
  data() {
    return {
      form: {
        name: "",
        phone: "",
        country: "",
        city: "",
        provinces: "",
        address: "",
        remark: "",
      },
      selectedOptions: [],
      options: regionData,
      loading: false,
      rules: {
        name: [{ required: true, message: "请输入客户名称", trigger: "blur" }],
        phone: [
          { required: true, message: "请输入客户电话", trigger: "blur" },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        city: [{ required: true, message: "请选择客户区域", trigger: "blur" }],
        address: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    if (this.$route.query.id) {
      this.getDetail();
    }
  },
  methods: {
    handleChange() {
      this.form.provinces = this.selectedOptions[0];
      this.form.city  = this.selectedOptions[1];
      this.form.country = this.selectedOptions[2];
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          addOrEditCustomer(this.form)
            .then((res) => {
              this.loading = false;
              this.$message.success("操作成功");
              this.$router.push("/customer/list");
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    getDetail() {
      getCustomerDetail(this.$route.query.id).then((res) => {
        this.form = res.data;
        this.selectedOptions = [
          this.form.provinces,
          this.form.city,
           this.form.country,
        ];
        // console.log(this.selectedOptions, "form");
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.form {
  width: 480px;
}
</style>
